package com.github.icloudpay.pay.core.rest;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.icloudpay.pay.core.biz.PayChannelBiz;
import com.github.icloudpay.pay.core.entity.PayChannel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.Query;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("admin/payChannel")
public class PayChannelController {

	
	@Autowired
	private PayChannelBiz biz;
	
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public TableResultResponse<PayChannel> queryPayChannel(@RequestParam(value = "payChannelName") String payChannelName, 
			@RequestParam(value = "page", defaultValue = "1") Integer page, 
			@RequestParam(value = "limit", defaultValue = "10") Integer limit) {
		Map<String, Object> param = new HashMap<>();
		param.put("payChannelName", payChannelName);
		param.put("page", page);
		param.put("limit", limit);
		log.info("查询支付通道列表，请求参数。。。{}"+param);
		Query query = new Query(param);
		Page<Object> result = PageHelper.startPage(query.getPage(), query.getLimit());
		List<PayChannel> list = biz.queryPayChannel(param);
		log.info("查询支付通道列表，响应参数.。。。{}"+list);
		return new TableResultResponse<>(result.getTotal(), list);
	}
	
	
	/**
	 * 详情
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	public ObjectRestResponse<PayChannel> queryPayChannelConfigInfo(@RequestBody Map<String, Object> params) {
		log.info("查询支付通道，请求参数.。。。{}"+params);
		ObjectRestResponse<PayChannel> response = new ObjectRestResponse<>();
		PayChannel config = biz.selectById(Long.valueOf((Integer) params.get("id")));
		response.data(config);
		log.info("查询支付通道，响应参数.。。。{}"+response);
		return response;
	}
	
	/**
	 * 删除
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ObjectRestResponse<PayChannel> deletePayChannel(@RequestBody Map<String, Object> params) {
		log.info("删除支付通道，请求参数。。。{}"+params);
		PayChannel config = new PayChannel();
		String payChannelNo = (String) params.get("payChannelNo");
		config.setPayChannelNo(payChannelNo);
		biz.delete(config);
		return new ObjectRestResponse<PayChannel>();
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ObjectRestResponse<PayChannel> addPayChannel(@RequestBody Map<String, Object> params) {
		log.info("添加支付通道，请求参数。。。{}"+params);
		PayChannel config = new PayChannel();
		String payChannelNo = (String) params.get("payChannelNo");
		String payChannelName = (String) params.get("payChannelName");
		String payCompanyNo = (String) params.get("payCompanyNo");
		String feeRatio = (String) params.get("feeRatioStr");
		String status = (String) params.get("status");
		String businessType = (String) params.get("businessType");
		config.setPayChannelNo(payChannelNo);
		config.setPayCompanyNo(payCompanyNo);
		config.setStatus(status);
		config.setBusinessType(businessType);
		BigDecimal b = new BigDecimal(feeRatio); 
//		b=b.setScale(2, BigDecimal.ROUND_DOWN);
		config.setFeeRatio(b);
		config.setPayChannelName(payChannelName);
		biz.insertSelective(config);
		return new ObjectRestResponse<PayChannel>();
	}
	
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ObjectRestResponse<PayChannel> updatePayChannelConfig(@RequestBody Map<String, Object> params) {
		log.info("更新支付通道，请求参数。。。{}"+params);
		String payChannelNo = (String) params.get("payChannelNo");
		String payChannelName = (String) params.get("payChannelName");
		String payCompanyNo = (String) params.get("payCompanyNo");
		String feeRatio = (String) params.get("feeRatioStr");
		String status = (String) params.get("status");
		String businessType = (String) params.get("businessType");
		Integer id = (Integer) params.get("id");
		PayChannel config = new PayChannel();
		config.setPayChannelNo(payChannelNo);
		config.setPayChannelName(payChannelName);
		config.setPayCompanyNo(payCompanyNo);
		config.setStatus(status);
		config.setBusinessType(businessType);
		config.setId(Long.valueOf(id));
		BigDecimal b = new BigDecimal(feeRatio); 
//		b = b.setScale(2, BigDecimal.ROUND_DOWN);
		config.setFeeRatio(b);
		biz.updateById(config);
		return new ObjectRestResponse<PayChannel>();
	}
	

}
